import mx.utils.Delegate;

import gugga.application.ApplicationController;
import gugga.application.NavigationManager;
import gugga.application.Section;
import gugga.navigation.MenuNavigation;
import gugga.navigation.NavigationButton;

import simpleSiteWithAdditionalNavigationButton.AboutSection;

/**
 * @author bony
 */ 
class simpleSiteWithAdditionalNavigationButton.Application 
	extends ApplicationController 

{
	//List all the application fields here. 
	private var mMainNavigation:MenuNavigation;
	
	private var mAboutButton:NavigationButton;
	private var mWhyButton:NavigationButton;
	private var mContactButton:NavigationButton;
	
	private var mAboutSection:AboutSection;
	private var mWhySection:Section;
	private var mContactSection:Section;
	
	//constructor	
	public function Application() 
	{
		//The constructor of the super class must be called.
		super();
		
		// The function getComponent creates a mapping between the instances on the scene and
		// the member variables of the Application class.
		mMainNavigation = getComponent("PrimaryNavigation");
		mAboutSection = getComponent("AboutSection");	
		mWhySection = getComponent("WhySection");	
		mContactSection = getComponent("ContactSection");	
	}
	
	// All Objects instantiated inside the Application inherit UIComponentEx except macromedia components.
	// This function is required because we have to make sure that 
	// all objects inside the Application are associated with their respective classes and are avaialable and ready to work.
	// All sections are phisically nested inside the main Application and their initialization is not immediate.
	// Before execution of that function you cannot call any event related methods or mixin related methods of the objects.
	
	public function initUI()
	{
		//The constructor of the super class must be called.
		super.initUI();
		
		// Configure main navigation.
		configureMenu();
		
		// Register all the sections in the application.
		registerSections();
		
		// Handle cross section navigation event.
		mAboutSection.addEventListener("navigate", Delegate.create(this, onAboutSectionNavigate));		
		
		// Open default section.				
		openSection("About");
		// Set navigation state. Should be set only the first time.
		NavigationManager.Instance.startNavigations();
		NavigationManager.Instance.markNavigations("About");
		
		// Triger initializing sequence.
		initialize();
			
		// Activating sequence		
		// Using default activating sequence.
		
		// Open sequence		
		// Using default open sequence.
		
		// Close sequence		
		// Using default close sequence.    		
	}
	public function configureMenu() 
	{
		// List all items used for navigation - each one must have it's own uniqueID.		
		mMainNavigation.registerChildSubItem("btnAbout","About","About");
		mMainNavigation.registerChildSubItem("btnWhy","Why","Why");
		mMainNavigation.registerChildSubItem("btnContact","Contact","Contact");
		
		NavigationManager.Instance.registerNavigation(mMainNavigation, "MainNavigation");	
	}
	
	public function registerSections()
	{
		registerChildSection("AboutSection","About","About");
		registerChildSection("WhySection","Why","Why");
		registerChildSection("ContactSection","Contact","Contact");
	}
	
	private function onAboutSectionNavigate(ev)
	{
		openSection(ev.sectionID);	
		NavigationManager.Instance.markNavigations(ev.sectionID);	
	}
}